package grailsworkshop2012_1_f

class Budget {
    Float food
    Float lodging
    Float tickets
    Float otherExpenses
    Float total
    //Se crea relación uno a uno con Trip
    Trip trip
        
    static constraints = {
        trip unique: true
        food(blank: false, validator: {qty -> qty >= 0} )
        lodging(blank: false, validator: {qty -> qty >= 0} )
        tickets(blank: false, validator: {qty -> qty >= 0} )
        otherExpenses(blank: false, validator: {qty -> qty >= 0} )
        total nullable: true, editable: false, display: false
    }
    
    //Disparador que añade inicializa el valor del atributo 'total'    
    def afterInsert() {
       total = food + lodging + tickets + otherExpenses
    }
    //Disparador actualiza el valor del atributo 'total'
    def afterUpdate() {
       total = food + lodging + tickets + otherExpenses
    }  
}
